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

libavformat/tmv.c

Go to the documentation of this file.
00001 /*
00002  * 8088flex TMV file demuxer
00003  * Copyright (c) 2009 Daniel Verkamp <daniel at drv.nu>
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 
00029 #include "libavutil/intreadwrite.h"
00030 #include "avformat.h"
00031 #include "internal.h"
00032 
00033 enum {
00034     TMV_PADDING = 0x01,
00035     TMV_STEREO  = 0x02,
00036 };
00037 
00038 #define TMV_TAG MKTAG('T', 'M', 'A', 'V')
00039 
00040 typedef struct TMVContext {
00041     unsigned audio_chunk_size;
00042     unsigned video_chunk_size;
00043     unsigned padding;
00044     unsigned stream_index;
00045 } TMVContext;
00046 
00047 #define TMV_HEADER_SIZE       12
00048 
00049 #define PROBE_MIN_SAMPLE_RATE 5000
00050 #define PROBE_MAX_FPS         120
00051 #define PROBE_MIN_AUDIO_SIZE  (PROBE_MIN_SAMPLE_RATE / PROBE_MAX_FPS)
00052 
00053 static int tmv_probe(AVProbeData *p)
00054 {
00055     if (AV_RL32(p->buf)   == TMV_TAG &&
00056         AV_RL16(p->buf+4) >= PROBE_MIN_SAMPLE_RATE &&
00057         AV_RL16(p->buf+6) >= PROBE_MIN_AUDIO_SIZE  &&
00058                !p->buf[8] && // compression method
00059                 p->buf[9] && // char cols
00060                 p->buf[10])  // char rows
00061         return AVPROBE_SCORE_MAX /
00062             ((p->buf[9] == 40 && p->buf[10] == 25) ? 1 : 4);
00063     return 0;
00064 }
00065 
00066 static int tmv_read_header(AVFormatContext *s, AVFormatParameters *ap)
00067 {
00068     TMVContext *tmv   = s->priv_data;
00069     AVIOContext *pb = s->pb;
00070     AVStream *vst, *ast;
00071     AVRational fps;
00072     unsigned comp_method, char_cols, char_rows, features;
00073 
00074     if (avio_rl32(pb) != TMV_TAG)
00075         return -1;
00076 
00077     if (!(vst = avformat_new_stream(s, NULL)))
00078         return AVERROR(ENOMEM);
00079 
00080     if (!(ast = avformat_new_stream(s, NULL)))
00081         return AVERROR(ENOMEM);
00082 
00083     ast->codec->sample_rate = avio_rl16(pb);
00084     if (!ast->codec->sample_rate) {
00085         av_log(s, AV_LOG_ERROR, "invalid sample rate\n");
00086         return -1;
00087     }
00088 
00089     tmv->audio_chunk_size   = avio_rl16(pb);
00090     if (!tmv->audio_chunk_size) {
00091         av_log(s, AV_LOG_ERROR, "invalid audio chunk size\n");
00092         return -1;
00093     }
00094 
00095     comp_method             = avio_r8(pb);
00096     if (comp_method) {
00097         av_log(s, AV_LOG_ERROR, "unsupported compression method %d\n",
00098                comp_method);
00099         return -1;
00100     }
00101 
00102     char_cols = avio_r8(pb);
00103     char_rows = avio_r8(pb);
00104     tmv->video_chunk_size = char_cols * char_rows * 2;
00105 
00106     features  = avio_r8(pb);
00107     if (features & ~(TMV_PADDING | TMV_STEREO)) {
00108         av_log(s, AV_LOG_ERROR, "unsupported features 0x%02x\n",
00109                features & ~(TMV_PADDING | TMV_STEREO));
00110         return -1;
00111     }
00112 
00113     ast->codec->codec_type            = AVMEDIA_TYPE_AUDIO;
00114     ast->codec->codec_id              = CODEC_ID_PCM_U8;
00115     ast->codec->channels              = features & TMV_STEREO ? 2 : 1;
00116     ast->codec->bits_per_coded_sample = 8;
00117     ast->codec->bit_rate              = ast->codec->sample_rate *
00118                                         ast->codec->bits_per_coded_sample;
00119     avpriv_set_pts_info(ast, 32, 1, ast->codec->sample_rate);
00120 
00121     fps.num = ast->codec->sample_rate * ast->codec->channels;
00122     fps.den = tmv->audio_chunk_size;
00123     av_reduce(&fps.num, &fps.den, fps.num, fps.den, 0xFFFFFFFFLL);
00124 
00125     vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00126     vst->codec->codec_id   = CODEC_ID_TMV;
00127     vst->codec->pix_fmt    = PIX_FMT_PAL8;
00128     vst->codec->width      = char_cols * 8;
00129     vst->codec->height     = char_rows * 8;
00130     avpriv_set_pts_info(vst, 32, fps.den, fps.num);
00131 
00132     if (features & TMV_PADDING)
00133         tmv->padding =
00134             ((tmv->video_chunk_size + tmv->audio_chunk_size + 511) & ~511) -
00135              (tmv->video_chunk_size + tmv->audio_chunk_size);
00136 
00137     vst->codec->bit_rate = ((tmv->video_chunk_size + tmv->padding) *
00138                             fps.num * 8) / fps.den;
00139 
00140     return 0;
00141 }
00142 
00143 static int tmv_read_packet(AVFormatContext *s, AVPacket *pkt)
00144 {
00145     TMVContext *tmv   = s->priv_data;
00146     AVIOContext *pb = s->pb;
00147     int ret, pkt_size = tmv->stream_index ?
00148                         tmv->audio_chunk_size : tmv->video_chunk_size;
00149 
00150     if (pb->eof_reached)
00151         return AVERROR_EOF;
00152 
00153     ret = av_get_packet(pb, pkt, pkt_size);
00154 
00155     if (tmv->stream_index)
00156         avio_skip(pb, tmv->padding);
00157 
00158     pkt->stream_index  = tmv->stream_index;
00159     tmv->stream_index ^= 1;
00160     pkt->flags        |= AV_PKT_FLAG_KEY;
00161 
00162     return ret;
00163 }
00164 
00165 static int tmv_read_seek(AVFormatContext *s, int stream_index,
00166                          int64_t timestamp, int flags)
00167 {
00168     TMVContext *tmv = s->priv_data;
00169     int64_t pos;
00170 
00171     if (stream_index)
00172         return -1;
00173 
00174     pos = timestamp *
00175           (tmv->audio_chunk_size + tmv->video_chunk_size + tmv->padding);
00176 
00177     avio_seek(s->pb, pos + TMV_HEADER_SIZE, SEEK_SET);
00178     tmv->stream_index = 0;
00179     return 0;
00180 }
00181 
00182 AVInputFormat ff_tmv_demuxer = {
00183     .name           = "tmv",
00184     .long_name      = NULL_IF_CONFIG_SMALL("8088flex TMV"),
00185     .priv_data_size = sizeof(TMVContext),
00186     .read_probe     = tmv_probe,
00187     .read_header    = tmv_read_header,
00188     .read_packet    = tmv_read_packet,
00189     .read_seek      = tmv_read_seek,
00190     .flags = AVFMT_GENERIC_INDEX,
00191 };
Generated on Thu Jan 24 2013 17:08:53 for Libav by doxygen 1.7.1