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 number = AV_RL32(&p->buf[0]);
00110 if ((number == 0) || (number > 1024))
00111 return 0;
00112
00113
00114 number = AV_RL32(&p->buf[4]);
00115 if ((number == 0) || (number > 1024))
00116 return 0;
00117
00118
00119 number = AV_RL32(&p->buf[8]);
00120 if ((number != 0) && ((number < 8000) | (number > 48000)))
00121 return 0;
00122
00123
00124 number = AV_RL32(&p->buf[12]);
00125 if (number > 2)
00126 return 0;
00127
00128
00129 number = AV_RL32(&p->buf[16]);
00130 if (number > 2)
00131 return 0;
00132
00133
00134 return AVPROBE_SCORE_MAX / 2;
00135 }
00136
00137 static int idcin_read_header(AVFormatContext *s,
00138 AVFormatParameters *ap)
00139 {
00140 ByteIOContext *pb = s->pb;
00141 IdcinDemuxContext *idcin = s->priv_data;
00142 AVStream *st;
00143 unsigned int width, height;
00144 unsigned int sample_rate, bytes_per_sample, channels;
00145
00146
00147 width = get_le32(pb);
00148 height = get_le32(pb);
00149 sample_rate = get_le32(pb);
00150 bytes_per_sample = get_le32(pb);
00151 channels = get_le32(pb);
00152
00153 st = av_new_stream(s, 0);
00154 if (!st)
00155 return AVERROR(ENOMEM);
00156 av_set_pts_info(st, 33, 1, IDCIN_FPS);
00157 idcin->video_stream_index = st->index;
00158 st->codec->codec_type = CODEC_TYPE_VIDEO;
00159 st->codec->codec_id = CODEC_ID_IDCIN;
00160 st->codec->codec_tag = 0;
00161 st->codec->width = width;
00162 st->codec->height = height;
00163
00164
00165 st->codec->extradata_size = HUFFMAN_TABLE_SIZE;
00166 st->codec->extradata = av_malloc(HUFFMAN_TABLE_SIZE);
00167 if (get_buffer(pb, st->codec->extradata, HUFFMAN_TABLE_SIZE) !=
00168 HUFFMAN_TABLE_SIZE)
00169 return AVERROR(EIO);
00170
00171 st->codec->palctrl = &idcin->palctrl;
00172
00173
00174 if (sample_rate) {
00175 idcin->audio_present = 1;
00176 st = av_new_stream(s, 0);
00177 if (!st)
00178 return AVERROR(ENOMEM);
00179 av_set_pts_info(st, 33, 1, IDCIN_FPS);
00180 idcin->audio_stream_index = st->index;
00181 st->codec->codec_type = CODEC_TYPE_AUDIO;
00182 st->codec->codec_tag = 1;
00183 st->codec->channels = channels;
00184 st->codec->sample_rate = sample_rate;
00185 st->codec->bits_per_coded_sample = bytes_per_sample * 8;
00186 st->codec->bit_rate = sample_rate * bytes_per_sample * 8 * channels;
00187 st->codec->block_align = bytes_per_sample * channels;
00188 if (bytes_per_sample == 1)
00189 st->codec->codec_id = CODEC_ID_PCM_U8;
00190 else
00191 st->codec->codec_id = CODEC_ID_PCM_S16LE;
00192
00193 if (sample_rate % 14 != 0) {
00194 idcin->audio_chunk_size1 = (sample_rate / 14) *
00195 bytes_per_sample * channels;
00196 idcin->audio_chunk_size2 = (sample_rate / 14 + 1) *
00197 bytes_per_sample * channels;
00198 } else {
00199 idcin->audio_chunk_size1 = idcin->audio_chunk_size2 =
00200 (sample_rate / 14) * bytes_per_sample * channels;
00201 }
00202 idcin->current_audio_chunk = 0;
00203 } else
00204 idcin->audio_present = 1;
00205
00206 idcin->next_chunk_is_video = 1;
00207 idcin->pts = 0;
00208
00209 return 0;
00210 }
00211
00212 static int idcin_read_packet(AVFormatContext *s,
00213 AVPacket *pkt)
00214 {
00215 int ret;
00216 unsigned int command;
00217 unsigned int chunk_size;
00218 IdcinDemuxContext *idcin = s->priv_data;
00219 ByteIOContext *pb = s->pb;
00220 int i;
00221 int palette_scale;
00222 unsigned char r, g, b;
00223 unsigned char palette_buffer[768];
00224
00225 if (url_feof(s->pb))
00226 return AVERROR(EIO);
00227
00228 if (idcin->next_chunk_is_video) {
00229 command = get_le32(pb);
00230 if (command == 2) {
00231 return AVERROR(EIO);
00232 } else if (command == 1) {
00233
00234 idcin->palctrl.palette_changed = 1;
00235 if (get_buffer(pb, palette_buffer, 768) != 768)
00236 return AVERROR(EIO);
00237
00238 palette_scale = 2;
00239 for (i = 0; i < 768; i++)
00240 if (palette_buffer[i] > 63) {
00241 palette_scale = 0;
00242 break;
00243 }
00244
00245 for (i = 0; i < 256; i++) {
00246 r = palette_buffer[i * 3 ] << palette_scale;
00247 g = palette_buffer[i * 3 + 1] << palette_scale;
00248 b = palette_buffer[i * 3 + 2] << palette_scale;
00249 idcin->palctrl.palette[i] = (r << 16) | (g << 8) | (b);
00250 }
00251 }
00252
00253 chunk_size = get_le32(pb);
00254
00255 url_fseek(pb, 4, SEEK_CUR);
00256 chunk_size -= 4;
00257 ret= av_get_packet(pb, pkt, chunk_size);
00258 if (ret != chunk_size)
00259 return AVERROR(EIO);
00260 pkt->stream_index = idcin->video_stream_index;
00261 pkt->pts = idcin->pts;
00262 } else {
00263
00264 if (idcin->current_audio_chunk)
00265 chunk_size = idcin->audio_chunk_size2;
00266 else
00267 chunk_size = idcin->audio_chunk_size1;
00268 ret= av_get_packet(pb, pkt, chunk_size);
00269 if (ret != chunk_size)
00270 return AVERROR(EIO);
00271 pkt->stream_index = idcin->audio_stream_index;
00272 pkt->pts = idcin->pts;
00273
00274 idcin->current_audio_chunk ^= 1;
00275 idcin->pts++;
00276 }
00277
00278 if (idcin->audio_present)
00279 idcin->next_chunk_is_video ^= 1;
00280
00281 return ret;
00282 }
00283
00284 AVInputFormat idcin_demuxer = {
00285 "idcin",
00286 NULL_IF_CONFIG_SMALL("id Cinematic format"),
00287 sizeof(IdcinDemuxContext),
00288 idcin_probe,
00289 idcin_read_header,
00290 idcin_read_packet,
00291 };