00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/intreadwrite.h"
00023 #include "libavcodec/bytestream.h"
00024 #include "avformat.h"
00025 #include "internal.h"
00026 #include "riff.h"
00027
00028 #define LXF_MAX_PACKET_HEADER_SIZE 256
00029 #define LXF_HEADER_DATA_SIZE 120
00030 #define LXF_IDENT "LEITCH\0"
00031 #define LXF_IDENT_LENGTH 8
00032 #define LXF_SAMPLERATE 48000
00033 #define LXF_MAX_AUDIO_PACKET (8008*15*4)
00034
00035 static const AVCodecTag lxf_tags[] = {
00036 { AV_CODEC_ID_MJPEG, 0 },
00037 { AV_CODEC_ID_MPEG1VIDEO, 1 },
00038 { AV_CODEC_ID_MPEG2VIDEO, 2 },
00039 { AV_CODEC_ID_MPEG2VIDEO, 3 },
00040 { AV_CODEC_ID_DVVIDEO, 4 },
00041 { AV_CODEC_ID_DVVIDEO, 5 },
00042 { AV_CODEC_ID_DVVIDEO, 6 },
00043 { AV_CODEC_ID_RAWVIDEO, 7 },
00044 { AV_CODEC_ID_RAWVIDEO, 8 },
00045 { AV_CODEC_ID_MPEG2VIDEO, 9 },
00046 { AV_CODEC_ID_NONE, 0 },
00047 };
00048
00049 typedef struct {
00050 int channels;
00051 uint8_t temp[LXF_MAX_AUDIO_PACKET];
00052 int frame_number;
00053 uint32_t video_format, packet_type, extended_size;
00054 } LXFDemuxContext;
00055
00056 static int lxf_probe(AVProbeData *p)
00057 {
00058 if (!memcmp(p->buf, LXF_IDENT, LXF_IDENT_LENGTH))
00059 return AVPROBE_SCORE_MAX;
00060
00061 return 0;
00062 }
00063
00070 static int check_checksum(const uint8_t *header, int size)
00071 {
00072 int x;
00073 uint32_t sum = 0;
00074
00075 for (x = 0; x < size; x += 4)
00076 sum += AV_RL32(&header[x]);
00077
00078 return sum;
00079 }
00080
00087 static int sync(AVFormatContext *s, uint8_t *header)
00088 {
00089 uint8_t buf[LXF_IDENT_LENGTH];
00090 int ret;
00091
00092 if ((ret = avio_read(s->pb, buf, LXF_IDENT_LENGTH)) != LXF_IDENT_LENGTH)
00093 return ret < 0 ? ret : AVERROR_EOF;
00094
00095 while (memcmp(buf, LXF_IDENT, LXF_IDENT_LENGTH)) {
00096 if (url_feof(s->pb))
00097 return AVERROR_EOF;
00098
00099 memmove(buf, &buf[1], LXF_IDENT_LENGTH-1);
00100 buf[LXF_IDENT_LENGTH-1] = avio_r8(s->pb);
00101 }
00102
00103 memcpy(header, LXF_IDENT, LXF_IDENT_LENGTH);
00104
00105 return 0;
00106 }
00107
00113 static int get_packet_header(AVFormatContext *s)
00114 {
00115 LXFDemuxContext *lxf = s->priv_data;
00116 AVIOContext *pb = s->pb;
00117 int track_size, samples, ret;
00118 uint32_t version, audio_format, header_size, channels, tmp;
00119 AVStream *st;
00120 uint8_t header[LXF_MAX_PACKET_HEADER_SIZE];
00121 const uint8_t *p;
00122
00123
00124 if ((ret = sync(s, header)) < 0)
00125 return ret;
00126
00127 ret = avio_read(pb, header + LXF_IDENT_LENGTH, 8);
00128 if (ret != 8)
00129 return ret < 0 ? ret : AVERROR_EOF;
00130
00131 p = header + LXF_IDENT_LENGTH;
00132 version = bytestream_get_le32(&p);
00133 header_size = bytestream_get_le32(&p);
00134 if (version > 1)
00135 av_log_ask_for_sample(s, "Unknown format version %i\n", version);
00136 if (header_size < (version ? 72 : 60) ||
00137 header_size > LXF_MAX_PACKET_HEADER_SIZE ||
00138 (header_size & 3)) {
00139 av_log(s, AV_LOG_ERROR, "Invalid header size 0x%x\n", header_size);
00140 return AVERROR_INVALIDDATA;
00141 }
00142
00143
00144 if ((ret = avio_read(pb, header + (p - header),
00145 header_size - (p - header))) !=
00146 header_size - (p - header)) {
00147 return ret < 0 ? ret : AVERROR_EOF;
00148 }
00149
00150 if (check_checksum(header, header_size))
00151 av_log(s, AV_LOG_ERROR, "checksum error\n");
00152
00153 lxf->packet_type = bytestream_get_le32(&p);
00154 p += version ? 20 : 12;
00155
00156 lxf->extended_size = 0;
00157 switch (lxf->packet_type) {
00158 case 0:
00159
00160 lxf->video_format = bytestream_get_le32(&p);
00161 ret = bytestream_get_le32(&p);
00162
00163 avio_skip(pb, (int64_t)(uint32_t)AV_RL32(p + 4) +
00164 (int64_t)(uint32_t)AV_RL32(p + 12));
00165 break;
00166 case 1:
00167
00168 if (!(st = s->streams[1])) {
00169 av_log(s, AV_LOG_INFO, "got audio packet, but no audio stream present\n");
00170 break;
00171 }
00172
00173 if (version == 0) p += 8;
00174 audio_format = bytestream_get_le32(&p);
00175 channels = bytestream_get_le32(&p);
00176 track_size = bytestream_get_le32(&p);
00177
00178
00179
00180 st->codec->bits_per_coded_sample = (audio_format >> 6) & 0x3F;
00181
00182 if (st->codec->bits_per_coded_sample != (audio_format & 0x3F)) {
00183 av_log(s, AV_LOG_WARNING, "only tightly packed PCM currently supported\n");
00184 return AVERROR_PATCHWELCOME;
00185 }
00186
00187 switch (st->codec->bits_per_coded_sample) {
00188 case 16: st->codec->codec_id = AV_CODEC_ID_PCM_S16LE; break;
00189 case 20: st->codec->codec_id = AV_CODEC_ID_PCM_LXF; break;
00190 case 24: st->codec->codec_id = AV_CODEC_ID_PCM_S24LE; break;
00191 case 32: st->codec->codec_id = AV_CODEC_ID_PCM_S32LE; break;
00192 default:
00193 av_log(s, AV_LOG_WARNING,
00194 "only 16-, 20-, 24- and 32-bit PCM currently supported\n");
00195 return AVERROR_PATCHWELCOME;
00196 }
00197
00198 samples = track_size * 8 / st->codec->bits_per_coded_sample;
00199
00200
00201
00202 if (samples == LXF_SAMPLERATE * 5005 / 30000) {
00203 avpriv_set_pts_info(s->streams[0], 64, 1001, 30000);
00204 } else {
00205
00206 if (samples != LXF_SAMPLERATE / 25)
00207 av_log(s, AV_LOG_WARNING,
00208 "video doesn't seem to be PAL or NTSC. guessing PAL\n");
00209
00210 avpriv_set_pts_info(s->streams[0], 64, 1, 25);
00211 }
00212
00213
00214 ret = av_popcount(channels) * track_size;
00215
00216 break;
00217 default:
00218 tmp = bytestream_get_le32(&p);
00219 ret = bytestream_get_le32(&p);
00220 if (tmp == 1)
00221 lxf->extended_size = bytestream_get_le32(&p);
00222 break;
00223 }
00224
00225 return ret;
00226 }
00227
00228 static int lxf_read_header(AVFormatContext *s)
00229 {
00230 LXFDemuxContext *lxf = s->priv_data;
00231 AVIOContext *pb = s->pb;
00232 uint8_t header_data[LXF_HEADER_DATA_SIZE];
00233 int ret;
00234 AVStream *st;
00235 uint32_t video_params, disk_params;
00236 uint16_t record_date, expiration_date;
00237
00238 if ((ret = get_packet_header(s)) < 0)
00239 return ret;
00240
00241 if (ret != LXF_HEADER_DATA_SIZE) {
00242 av_log(s, AV_LOG_ERROR, "expected %d B size header, got %d\n",
00243 LXF_HEADER_DATA_SIZE, ret);
00244 return AVERROR_INVALIDDATA;
00245 }
00246
00247 if ((ret = avio_read(pb, header_data, LXF_HEADER_DATA_SIZE)) != LXF_HEADER_DATA_SIZE)
00248 return ret < 0 ? ret : AVERROR_EOF;
00249
00250 if (!(st = avformat_new_stream(s, NULL)))
00251 return AVERROR(ENOMEM);
00252
00253 st->duration = AV_RL32(&header_data[32]);
00254 video_params = AV_RL32(&header_data[40]);
00255 record_date = AV_RL16(&header_data[56]);
00256 expiration_date = AV_RL16(&header_data[58]);
00257 disk_params = AV_RL32(&header_data[116]);
00258
00259 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00260 st->codec->bit_rate = 1000000 * ((video_params >> 14) & 0xFF);
00261 st->codec->codec_tag = video_params & 0xF;
00262 st->codec->codec_id = ff_codec_get_id(lxf_tags, st->codec->codec_tag);
00263
00264 av_log(s, AV_LOG_DEBUG, "record: %x = %i-%02i-%02i\n",
00265 record_date, 1900 + (record_date & 0x7F), (record_date >> 7) & 0xF,
00266 (record_date >> 11) & 0x1F);
00267
00268 av_log(s, AV_LOG_DEBUG, "expire: %x = %i-%02i-%02i\n",
00269 expiration_date, 1900 + (expiration_date & 0x7F), (expiration_date >> 7) & 0xF,
00270 (expiration_date >> 11) & 0x1F);
00271
00272 if ((video_params >> 22) & 1)
00273 av_log(s, AV_LOG_WARNING, "VBI data not yet supported\n");
00274
00275 if ((lxf->channels = 1 << (disk_params >> 4 & 3) + 1)) {
00276 if (!(st = avformat_new_stream(s, NULL)))
00277 return AVERROR(ENOMEM);
00278
00279 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00280 st->codec->sample_rate = LXF_SAMPLERATE;
00281 st->codec->channels = lxf->channels;
00282
00283 avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
00284 }
00285
00286 avio_skip(s->pb, lxf->extended_size);
00287
00288 return 0;
00289 }
00290
00298 static void deplanarize(LXFDemuxContext *lxf, AVStream *ast, uint8_t *out, int bytes)
00299 {
00300 int x, y, z, i, bytes_per_sample = ast->codec->bits_per_coded_sample >> 3;
00301
00302 for (z = i = 0; z < lxf->channels; z++)
00303 for (y = 0; y < bytes / bytes_per_sample / lxf->channels; y++)
00304 for (x = 0; x < bytes_per_sample; x++, i++)
00305 out[x + bytes_per_sample*(z + y*lxf->channels)] = lxf->temp[i];
00306 }
00307
00308 static int lxf_read_packet(AVFormatContext *s, AVPacket *pkt)
00309 {
00310 LXFDemuxContext *lxf = s->priv_data;
00311 AVIOContext *pb = s->pb;
00312 uint8_t *buf;
00313 AVStream *ast = NULL;
00314 uint32_t stream;
00315 int ret, ret2;
00316
00317 if ((ret = get_packet_header(s)) < 0)
00318 return ret;
00319
00320 stream = lxf->packet_type;
00321
00322 if (stream > 1) {
00323 av_log(s, AV_LOG_WARNING, "got packet with illegal stream index %u\n", stream);
00324 return AVERROR(EAGAIN);
00325 }
00326
00327 if (stream == 1 && !(ast = s->streams[1])) {
00328 av_log(s, AV_LOG_ERROR, "got audio packet without having an audio stream\n");
00329 return AVERROR_INVALIDDATA;
00330 }
00331
00332
00333 if (ast && ret > LXF_MAX_AUDIO_PACKET) {
00334 av_log(s, AV_LOG_ERROR, "audio packet too large (%i > %i)\n",
00335 ret, LXF_MAX_AUDIO_PACKET);
00336 return AVERROR_INVALIDDATA;
00337 }
00338
00339 if ((ret2 = av_new_packet(pkt, ret)) < 0)
00340 return ret2;
00341
00342
00343 buf = ast && ast->codec->codec_id != AV_CODEC_ID_PCM_LXF ? lxf->temp : pkt->data;
00344
00345 if ((ret2 = avio_read(pb, buf, ret)) != ret) {
00346 av_free_packet(pkt);
00347 return ret2 < 0 ? ret2 : AVERROR_EOF;
00348 }
00349
00350 pkt->stream_index = stream;
00351
00352 if (ast) {
00353 if(ast->codec->codec_id != AV_CODEC_ID_PCM_LXF)
00354 deplanarize(lxf, ast, pkt->data, ret);
00355 } else {
00356
00357 if (((lxf->video_format >> 22) & 0x3) < 2)
00358 pkt->flags |= AV_PKT_FLAG_KEY;
00359
00360 pkt->dts = lxf->frame_number++;
00361 }
00362
00363 return ret;
00364 }
00365
00366 AVInputFormat ff_lxf_demuxer = {
00367 .name = "lxf",
00368 .long_name = NULL_IF_CONFIG_SMALL("VR native stream (LXF)"),
00369 .priv_data_size = sizeof(LXFDemuxContext),
00370 .read_probe = lxf_probe,
00371 .read_header = lxf_read_header,
00372 .read_packet = lxf_read_packet,
00373 .codec_tag = (const AVCodecTag* const []){lxf_tags, 0},
00374 };