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 #include "internal.h"
00074
00075 #define HUFFMAN_TABLE_SIZE (64 * 1024)
00076 #define IDCIN_FPS 14
00077
00078 typedef struct IdcinDemuxContext {
00079 int video_stream_index;
00080 int audio_stream_index;
00081 int audio_chunk_size1;
00082 int audio_chunk_size2;
00083
00084
00085 int current_audio_chunk;
00086 int next_chunk_is_video;
00087 int audio_present;
00088
00089 int64_t pts;
00090 } IdcinDemuxContext;
00091
00092 static int idcin_probe(AVProbeData *p)
00093 {
00094 unsigned int number;
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109 if (p->buf_size < 20)
00110 return 0;
00111
00112
00113 number = AV_RL32(&p->buf[0]);
00114 if ((number == 0) || (number > 1024))
00115 return 0;
00116
00117
00118 number = AV_RL32(&p->buf[4]);
00119 if ((number == 0) || (number > 1024))
00120 return 0;
00121
00122
00123 number = AV_RL32(&p->buf[8]);
00124 if ((number != 0) && ((number < 8000) | (number > 48000)))
00125 return 0;
00126
00127
00128 number = AV_RL32(&p->buf[12]);
00129 if (number > 2)
00130 return 0;
00131
00132
00133 number = AV_RL32(&p->buf[16]);
00134 if (number > 2)
00135 return 0;
00136
00137
00138 return AVPROBE_SCORE_MAX / 2;
00139 }
00140
00141 static int idcin_read_header(AVFormatContext *s)
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 = avformat_new_stream(s, NULL);
00157 if (!st)
00158 return AVERROR(ENOMEM);
00159 avpriv_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 = avformat_new_stream(s, NULL);
00178 if (!st)
00179 return AVERROR(ENOMEM);
00180 avpriv_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] = (0xFFU << 24) | (r << 16) | (g << 8) | (b);
00251 if (palette_scale == 2)
00252 palette[i] |= palette[i] >> 6 & 0x30303;
00253 }
00254 }
00255
00256 chunk_size = avio_rl32(pb);
00257
00258 avio_skip(pb, 4);
00259 chunk_size -= 4;
00260 ret= av_get_packet(pb, pkt, chunk_size);
00261 if (ret < 0)
00262 return ret;
00263 if (command == 1) {
00264 uint8_t *pal;
00265
00266 pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE,
00267 AVPALETTE_SIZE);
00268 if (!pal)
00269 return AVERROR(ENOMEM);
00270 memcpy(pal, palette, AVPALETTE_SIZE);
00271 }
00272 pkt->stream_index = idcin->video_stream_index;
00273 pkt->pts = idcin->pts;
00274 } else {
00275
00276 if (idcin->current_audio_chunk)
00277 chunk_size = idcin->audio_chunk_size2;
00278 else
00279 chunk_size = idcin->audio_chunk_size1;
00280 ret= av_get_packet(pb, pkt, chunk_size);
00281 if (ret < 0)
00282 return ret;
00283 pkt->stream_index = idcin->audio_stream_index;
00284 pkt->pts = idcin->pts;
00285
00286 idcin->current_audio_chunk ^= 1;
00287 idcin->pts++;
00288 }
00289
00290 if (idcin->audio_present)
00291 idcin->next_chunk_is_video ^= 1;
00292
00293 return ret;
00294 }
00295
00296 AVInputFormat ff_idcin_demuxer = {
00297 .name = "idcin",
00298 .long_name = NULL_IF_CONFIG_SMALL("id Cinematic format"),
00299 .priv_data_size = sizeof(IdcinDemuxContext),
00300 .read_probe = idcin_probe,
00301 .read_header = idcin_read_header,
00302 .read_packet = idcin_read_packet,
00303 };