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 AVFormatParameters *ap)
00143 {
00144 AVIOContext *pb = s->pb;
00145 IdcinDemuxContext *idcin = s->priv_data;
00146 AVStream *st;
00147 unsigned int width, height;
00148 unsigned int sample_rate, bytes_per_sample, channels;
00149
00150
00151 width = avio_rl32(pb);
00152 height = avio_rl32(pb);
00153 sample_rate = avio_rl32(pb);
00154 bytes_per_sample = avio_rl32(pb);
00155 channels = avio_rl32(pb);
00156
00157 st = avformat_new_stream(s, NULL);
00158 if (!st)
00159 return AVERROR(ENOMEM);
00160 avpriv_set_pts_info(st, 33, 1, IDCIN_FPS);
00161 idcin->video_stream_index = st->index;
00162 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00163 st->codec->codec_id = CODEC_ID_IDCIN;
00164 st->codec->codec_tag = 0;
00165 st->codec->width = width;
00166 st->codec->height = height;
00167
00168
00169 st->codec->extradata_size = HUFFMAN_TABLE_SIZE;
00170 st->codec->extradata = av_malloc(HUFFMAN_TABLE_SIZE);
00171 if (avio_read(pb, st->codec->extradata, HUFFMAN_TABLE_SIZE) !=
00172 HUFFMAN_TABLE_SIZE)
00173 return AVERROR(EIO);
00174
00175
00176 if (sample_rate) {
00177 idcin->audio_present = 1;
00178 st = avformat_new_stream(s, NULL);
00179 if (!st)
00180 return AVERROR(ENOMEM);
00181 avpriv_set_pts_info(st, 33, 1, IDCIN_FPS);
00182 idcin->audio_stream_index = st->index;
00183 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00184 st->codec->codec_tag = 1;
00185 st->codec->channels = channels;
00186 st->codec->sample_rate = sample_rate;
00187 st->codec->bits_per_coded_sample = bytes_per_sample * 8;
00188 st->codec->bit_rate = sample_rate * bytes_per_sample * 8 * channels;
00189 st->codec->block_align = bytes_per_sample * channels;
00190 if (bytes_per_sample == 1)
00191 st->codec->codec_id = CODEC_ID_PCM_U8;
00192 else
00193 st->codec->codec_id = CODEC_ID_PCM_S16LE;
00194
00195 if (sample_rate % 14 != 0) {
00196 idcin->audio_chunk_size1 = (sample_rate / 14) *
00197 bytes_per_sample * channels;
00198 idcin->audio_chunk_size2 = (sample_rate / 14 + 1) *
00199 bytes_per_sample * channels;
00200 } else {
00201 idcin->audio_chunk_size1 = idcin->audio_chunk_size2 =
00202 (sample_rate / 14) * bytes_per_sample * channels;
00203 }
00204 idcin->current_audio_chunk = 0;
00205 } else
00206 idcin->audio_present = 1;
00207
00208 idcin->next_chunk_is_video = 1;
00209 idcin->pts = 0;
00210
00211 return 0;
00212 }
00213
00214 static int idcin_read_packet(AVFormatContext *s,
00215 AVPacket *pkt)
00216 {
00217 int ret;
00218 unsigned int command;
00219 unsigned int chunk_size;
00220 IdcinDemuxContext *idcin = s->priv_data;
00221 AVIOContext *pb = s->pb;
00222 int i;
00223 int palette_scale;
00224 unsigned char r, g, b;
00225 unsigned char palette_buffer[768];
00226 uint32_t palette[256];
00227
00228 if (url_feof(s->pb))
00229 return AVERROR(EIO);
00230
00231 if (idcin->next_chunk_is_video) {
00232 command = avio_rl32(pb);
00233 if (command == 2) {
00234 return AVERROR(EIO);
00235 } else if (command == 1) {
00236
00237 if (avio_read(pb, palette_buffer, 768) != 768)
00238 return AVERROR(EIO);
00239
00240 palette_scale = 2;
00241 for (i = 0; i < 768; i++)
00242 if (palette_buffer[i] > 63) {
00243 palette_scale = 0;
00244 break;
00245 }
00246
00247 for (i = 0; i < 256; i++) {
00248 r = palette_buffer[i * 3 ] << palette_scale;
00249 g = palette_buffer[i * 3 + 1] << palette_scale;
00250 b = palette_buffer[i * 3 + 2] << palette_scale;
00251 palette[i] = (0xFFU << 24) | (r << 16) | (g << 8) | (b);
00252 if (palette_scale == 2)
00253 palette[i] |= palette[i] >> 6 & 0x30303;
00254 }
00255 }
00256
00257 chunk_size = avio_rl32(pb);
00258
00259 avio_skip(pb, 4);
00260 chunk_size -= 4;
00261 ret= av_get_packet(pb, pkt, chunk_size);
00262 if (ret < 0)
00263 return ret;
00264 if (command == 1) {
00265 uint8_t *pal;
00266
00267 pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE,
00268 AVPALETTE_SIZE);
00269 if (!pal)
00270 return AVERROR(ENOMEM);
00271 memcpy(pal, palette, AVPALETTE_SIZE);
00272 }
00273 pkt->stream_index = idcin->video_stream_index;
00274 pkt->pts = idcin->pts;
00275 } else {
00276
00277 if (idcin->current_audio_chunk)
00278 chunk_size = idcin->audio_chunk_size2;
00279 else
00280 chunk_size = idcin->audio_chunk_size1;
00281 ret= av_get_packet(pb, pkt, chunk_size);
00282 if (ret < 0)
00283 return ret;
00284 pkt->stream_index = idcin->audio_stream_index;
00285 pkt->pts = idcin->pts;
00286
00287 idcin->current_audio_chunk ^= 1;
00288 idcin->pts++;
00289 }
00290
00291 if (idcin->audio_present)
00292 idcin->next_chunk_is_video ^= 1;
00293
00294 return ret;
00295 }
00296
00297 AVInputFormat ff_idcin_demuxer = {
00298 .name = "idcin",
00299 .long_name = NULL_IF_CONFIG_SMALL("id Cinematic format"),
00300 .priv_data_size = sizeof(IdcinDemuxContext),
00301 .read_probe = idcin_probe,
00302 .read_header = idcin_read_header,
00303 .read_packet = idcin_read_packet,
00304 };