• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavformat/lxfdec.c

Go to the documentation of this file.
00001 /*
00002  * LXF demuxer
00003  * Copyright (c) 2010 Tomas Härdin
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * Libav is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with Libav; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00022 #include "libavutil/intreadwrite.h"
00023 #include "avformat.h"
00024 #include "internal.h"
00025 #include "riff.h"
00026 
00027 #define LXF_PACKET_HEADER_SIZE  60
00028 #define LXF_HEADER_DATA_SIZE    120
00029 #define LXF_IDENT               "LEITCH\0"
00030 #define LXF_IDENT_LENGTH        8
00031 #define LXF_SAMPLERATE          48000
00032 #define LXF_MAX_AUDIO_PACKET    (8008*15*4) ///< 15-channel 32-bit NTSC audio frame
00033 
00034 static const AVCodecTag lxf_tags[] = {
00035     { CODEC_ID_MJPEG,       0 },
00036     { CODEC_ID_MPEG1VIDEO,  1 },
00037     { CODEC_ID_MPEG2VIDEO,  2 },    //MpMl, 4:2:0
00038     { CODEC_ID_MPEG2VIDEO,  3 },    //MpPl, 4:2:2
00039     { CODEC_ID_DVVIDEO,     4 },    //DV25
00040     { CODEC_ID_DVVIDEO,     5 },    //DVCPRO
00041     { CODEC_ID_DVVIDEO,     6 },    //DVCPRO50
00042     { CODEC_ID_RAWVIDEO,    7 },    //PIX_FMT_ARGB, where alpha is used for chroma keying
00043     { CODEC_ID_RAWVIDEO,    8 },    //16-bit chroma key
00044     { CODEC_ID_MPEG2VIDEO,  9 },    //4:2:2 CBP ("Constrained Bytes per Gop")
00045     { CODEC_ID_NONE,        0 },
00046 };
00047 
00048 typedef struct {
00049     int channels;                       
00050     uint8_t temp[LXF_MAX_AUDIO_PACKET]; 
00051     int frame_number;                   
00052 } LXFDemuxContext;
00053 
00054 static int lxf_probe(AVProbeData *p)
00055 {
00056     if (!memcmp(p->buf, LXF_IDENT, LXF_IDENT_LENGTH))
00057         return AVPROBE_SCORE_MAX;
00058 
00059     return 0;
00060 }
00061 
00068 static int check_checksum(const uint8_t *header)
00069 {
00070     int x;
00071     uint32_t sum = 0;
00072 
00073     for (x = 0; x < LXF_PACKET_HEADER_SIZE; x += 4)
00074         sum += AV_RL32(&header[x]);
00075 
00076     return sum;
00077 }
00078 
00085 static int sync(AVFormatContext *s, uint8_t *header)
00086 {
00087     uint8_t buf[LXF_IDENT_LENGTH];
00088     int ret;
00089 
00090     if ((ret = avio_read(s->pb, buf, LXF_IDENT_LENGTH)) != LXF_IDENT_LENGTH)
00091         return ret < 0 ? ret : AVERROR_EOF;
00092 
00093     while (memcmp(buf, LXF_IDENT, LXF_IDENT_LENGTH)) {
00094         if (s->pb->eof_reached)
00095             return AVERROR_EOF;
00096 
00097         memmove(buf, &buf[1], LXF_IDENT_LENGTH-1);
00098         buf[LXF_IDENT_LENGTH-1] = avio_r8(s->pb);
00099     }
00100 
00101     memcpy(header, LXF_IDENT, LXF_IDENT_LENGTH);
00102 
00103     return 0;
00104 }
00105 
00113 static int get_packet_header(AVFormatContext *s, uint8_t *header, uint32_t *format)
00114 {
00115     AVIOContext   *pb  = s->pb;
00116     int track_size, samples, ret;
00117     AVStream *st;
00118 
00119     //find and read the ident
00120     if ((ret = sync(s, header)) < 0)
00121         return ret;
00122 
00123     //read the rest of the packet header
00124     if ((ret = avio_read(pb, header + LXF_IDENT_LENGTH,
00125                           LXF_PACKET_HEADER_SIZE - LXF_IDENT_LENGTH)) !=
00126                           LXF_PACKET_HEADER_SIZE - LXF_IDENT_LENGTH) {
00127         return ret < 0 ? ret : AVERROR_EOF;
00128     }
00129 
00130     if (check_checksum(header))
00131         av_log(s, AV_LOG_ERROR, "checksum error\n");
00132 
00133     *format = AV_RL32(&header[32]);
00134     ret     = AV_RL32(&header[36]);
00135 
00136     //type
00137     switch (AV_RL32(&header[16])) {
00138     case 0:
00139         //video
00140         //skip VBI data and metadata
00141         avio_skip(pb, (int64_t)(uint32_t)AV_RL32(&header[44]) +
00142                       (int64_t)(uint32_t)AV_RL32(&header[52]));
00143         break;
00144     case 1:
00145         //audio
00146         if (!(st = s->streams[1])) {
00147             av_log(s, AV_LOG_INFO, "got audio packet, but no audio stream present\n");
00148             break;
00149         }
00150 
00151         //set codec based on specified audio bitdepth
00152         //we only support tightly packed 16-, 20-, 24- and 32-bit PCM at the moment
00153         *format                          = AV_RL32(&header[40]);
00154         st->codec->bits_per_coded_sample = (*format >> 6) & 0x3F;
00155 
00156         if (st->codec->bits_per_coded_sample != (*format & 0x3F)) {
00157             av_log(s, AV_LOG_WARNING, "only tightly packed PCM currently supported\n");
00158             return AVERROR_PATCHWELCOME;
00159         }
00160 
00161         switch (st->codec->bits_per_coded_sample) {
00162         case 16: st->codec->codec_id = CODEC_ID_PCM_S16LE; break;
00163         case 20: st->codec->codec_id = CODEC_ID_PCM_LXF;   break;
00164         case 24: st->codec->codec_id = CODEC_ID_PCM_S24LE; break;
00165         case 32: st->codec->codec_id = CODEC_ID_PCM_S32LE; break;
00166         default:
00167             av_log(s, AV_LOG_WARNING,
00168                    "only 16-, 20-, 24- and 32-bit PCM currently supported\n");
00169             return AVERROR_PATCHWELCOME;
00170         }
00171 
00172         track_size = AV_RL32(&header[48]);
00173         samples = track_size * 8 / st->codec->bits_per_coded_sample;
00174 
00175         //use audio packet size to determine video standard
00176         //for NTSC we have one 8008-sample audio frame per five video frames
00177         if (samples == LXF_SAMPLERATE * 5005 / 30000) {
00178             avpriv_set_pts_info(s->streams[0], 64, 1001, 30000);
00179         } else {
00180             //assume PAL, but warn if we don't have 1920 samples
00181             if (samples != LXF_SAMPLERATE / 25)
00182                 av_log(s, AV_LOG_WARNING,
00183                        "video doesn't seem to be PAL or NTSC. guessing PAL\n");
00184 
00185             avpriv_set_pts_info(s->streams[0], 64, 1, 25);
00186         }
00187 
00188         //TODO: warning if track mask != (1 << channels) - 1?
00189         ret = av_popcount(AV_RL32(&header[44])) * track_size;
00190 
00191         break;
00192     default:
00193         break;
00194     }
00195 
00196     return ret;
00197 }
00198 
00199 static int lxf_read_header(AVFormatContext *s, AVFormatParameters *ap)
00200 {
00201     LXFDemuxContext *lxf = s->priv_data;
00202     AVIOContext   *pb  = s->pb;
00203     uint8_t header[LXF_PACKET_HEADER_SIZE], header_data[LXF_HEADER_DATA_SIZE];
00204     int ret;
00205     AVStream *st;
00206     uint32_t format, video_params, disk_params;
00207     uint16_t record_date, expiration_date;
00208 
00209     if ((ret = get_packet_header(s, header, &format)) < 0)
00210         return ret;
00211 
00212     if (ret != LXF_HEADER_DATA_SIZE) {
00213         av_log(s, AV_LOG_ERROR, "expected %d B size header, got %d\n",
00214                LXF_HEADER_DATA_SIZE, ret);
00215         return AVERROR_INVALIDDATA;
00216     }
00217 
00218     if ((ret = avio_read(pb, header_data, LXF_HEADER_DATA_SIZE)) != LXF_HEADER_DATA_SIZE)
00219         return ret < 0 ? ret : AVERROR_EOF;
00220 
00221     if (!(st = avformat_new_stream(s, NULL)))
00222         return AVERROR(ENOMEM);
00223 
00224     st->duration          = AV_RL32(&header_data[32]);
00225     video_params          = AV_RL32(&header_data[40]);
00226     record_date           = AV_RL16(&header_data[56]);
00227     expiration_date       = AV_RL16(&header_data[58]);
00228     disk_params           = AV_RL32(&header_data[116]);
00229 
00230     st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00231     st->codec->bit_rate   = 1000000 * ((video_params >> 14) & 0xFF);
00232     st->codec->codec_tag  = video_params & 0xF;
00233     st->codec->codec_id   = ff_codec_get_id(lxf_tags, st->codec->codec_tag);
00234 
00235     av_log(s, AV_LOG_DEBUG, "record: %x = %i-%02i-%02i\n",
00236            record_date, 1900 + (record_date & 0x7F), (record_date >> 7) & 0xF,
00237            (record_date >> 11) & 0x1F);
00238 
00239     av_log(s, AV_LOG_DEBUG, "expire: %x = %i-%02i-%02i\n",
00240            expiration_date, 1900 + (expiration_date & 0x7F), (expiration_date >> 7) & 0xF,
00241            (expiration_date >> 11) & 0x1F);
00242 
00243     if ((video_params >> 22) & 1)
00244         av_log(s, AV_LOG_WARNING, "VBI data not yet supported\n");
00245 
00246     if ((lxf->channels = (disk_params >> 2) & 0xF)) {
00247         if (!(st = avformat_new_stream(s, NULL)))
00248             return AVERROR(ENOMEM);
00249 
00250         st->codec->codec_type  = AVMEDIA_TYPE_AUDIO;
00251         st->codec->sample_rate = LXF_SAMPLERATE;
00252         st->codec->channels    = lxf->channels;
00253 
00254         avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
00255     }
00256 
00257     if (format == 1) {
00258         //skip extended field data
00259         avio_skip(s->pb, (uint32_t)AV_RL32(&header[40]));
00260     }
00261 
00262     return 0;
00263 }
00264 
00272 static void deplanarize(LXFDemuxContext *lxf, AVStream *ast, uint8_t *out, int bytes)
00273 {
00274     int x, y, z, i, bytes_per_sample = ast->codec->bits_per_coded_sample >> 3;
00275 
00276     for (z = i = 0; z < lxf->channels; z++)
00277         for (y = 0; y < bytes / bytes_per_sample / lxf->channels; y++)
00278             for (x = 0; x < bytes_per_sample; x++, i++)
00279                 out[x + bytes_per_sample*(z + y*lxf->channels)] = lxf->temp[i];
00280 }
00281 
00282 static int lxf_read_packet(AVFormatContext *s, AVPacket *pkt)
00283 {
00284     LXFDemuxContext *lxf = s->priv_data;
00285     AVIOContext   *pb  = s->pb;
00286     uint8_t header[LXF_PACKET_HEADER_SIZE], *buf;
00287     AVStream *ast = NULL;
00288     uint32_t stream, format;
00289     int ret, ret2;
00290 
00291     if ((ret = get_packet_header(s, header, &format)) < 0)
00292         return ret;
00293 
00294     stream = AV_RL32(&header[16]);
00295 
00296     if (stream > 1) {
00297         av_log(s, AV_LOG_WARNING, "got packet with illegal stream index %u\n", stream);
00298         return AVERROR(EAGAIN);
00299     }
00300 
00301     if (stream == 1 && !(ast = s->streams[1])) {
00302         av_log(s, AV_LOG_ERROR, "got audio packet without having an audio stream\n");
00303         return AVERROR_INVALIDDATA;
00304     }
00305 
00306     //make sure the data fits in the de-planerization buffer
00307     if (ast && ret > LXF_MAX_AUDIO_PACKET) {
00308         av_log(s, AV_LOG_ERROR, "audio packet too large (%i > %i)\n",
00309             ret, LXF_MAX_AUDIO_PACKET);
00310         return AVERROR_INVALIDDATA;
00311     }
00312 
00313     if ((ret2 = av_new_packet(pkt, ret)) < 0)
00314         return ret2;
00315 
00316     //read non-20-bit audio data into lxf->temp so we can deplanarize it
00317     buf = ast && ast->codec->codec_id != CODEC_ID_PCM_LXF ? lxf->temp : pkt->data;
00318 
00319     if ((ret2 = avio_read(pb, buf, ret)) != ret) {
00320         av_free_packet(pkt);
00321         return ret2 < 0 ? ret2 : AVERROR_EOF;
00322     }
00323 
00324     pkt->stream_index = stream;
00325 
00326     if (ast) {
00327         if(ast->codec->codec_id != CODEC_ID_PCM_LXF)
00328             deplanarize(lxf, ast, pkt->data, ret);
00329     } else {
00330         //picture type (0 = closed I, 1 = open I, 2 = P, 3 = B)
00331         if (((format >> 22) & 0x3) < 2)
00332             pkt->flags |= AV_PKT_FLAG_KEY;
00333 
00334         pkt->dts = lxf->frame_number++;
00335     }
00336 
00337     return ret;
00338 }
00339 
00340 AVInputFormat ff_lxf_demuxer = {
00341     .name           = "lxf",
00342     .long_name      = NULL_IF_CONFIG_SMALL("VR native stream format (LXF)"),
00343     .priv_data_size = sizeof(LXFDemuxContext),
00344     .read_probe     = lxf_probe,
00345     .read_header    = lxf_read_header,
00346     .read_packet    = lxf_read_packet,
00347     .codec_tag      = (const AVCodecTag* const []){lxf_tags, 0},
00348 };
00349 
Generated on Thu Jul 11 2013 15:38:23 for Libav by doxygen 1.7.1