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