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

libavformat/pva.c

Go to the documentation of this file.
00001 /*
00002  * TechnoTrend PVA (.pva) demuxer
00003  * Copyright (c) 2007, 2008 Ivo van Poorten
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00022 #include "avformat.h"
00023 #include "internal.h"
00024 #include "mpeg.h"
00025 
00026 #define PVA_MAX_PAYLOAD_LENGTH  0x17f8
00027 #define PVA_VIDEO_PAYLOAD       0x01
00028 #define PVA_AUDIO_PAYLOAD       0x02
00029 #define PVA_MAGIC               (('A' << 8) + 'V')
00030 
00031 typedef struct {
00032     int continue_pes;
00033 } PVAContext;
00034 
00035 static int pva_check(uint8_t *p) {
00036     int length = AV_RB16(p + 6);
00037     if (AV_RB16(p) != PVA_MAGIC || !p[2] || p[2] > 2 || p[4] != 0x55 ||
00038         (p[5] & 0xe0) || length > PVA_MAX_PAYLOAD_LENGTH)
00039         return -1;
00040     return length + 8;
00041 }
00042 
00043 static int pva_probe(AVProbeData * pd) {
00044     unsigned char *buf = pd->buf;
00045     int len = pva_check(buf);
00046 
00047     if (len < 0)
00048         return 0;
00049 
00050     if (pd->buf_size >= len + 8 &&
00051         pva_check(buf + len) >= 0)
00052         return AVPROBE_SCORE_MAX / 2;
00053 
00054     return AVPROBE_SCORE_MAX / 4;
00055 }
00056 
00057 static int pva_read_header(AVFormatContext *s, AVFormatParameters *ap) {
00058     AVStream *st;
00059 
00060     if (!(st = avformat_new_stream(s, NULL)))
00061         return AVERROR(ENOMEM);
00062     st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00063     st->codec->codec_id   = CODEC_ID_MPEG2VIDEO;
00064     st->need_parsing      = AVSTREAM_PARSE_FULL;
00065     avpriv_set_pts_info(st, 32, 1, 90000);
00066     av_add_index_entry(st, 0, 0, 0, 0, AVINDEX_KEYFRAME);
00067 
00068     if (!(st = avformat_new_stream(s, NULL)))
00069         return AVERROR(ENOMEM);
00070     st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00071     st->codec->codec_id   = CODEC_ID_MP2;
00072     st->need_parsing      = AVSTREAM_PARSE_FULL;
00073     avpriv_set_pts_info(st, 33, 1, 90000);
00074     av_add_index_entry(st, 0, 0, 0, 0, AVINDEX_KEYFRAME);
00075 
00076     /* the parameters will be extracted from the compressed bitstream */
00077     return 0;
00078 }
00079 
00080 #define pva_log if (read_packet) av_log
00081 
00082 static int read_part_of_packet(AVFormatContext *s, int64_t *pts,
00083                                int *len, int *strid, int read_packet) {
00084     AVIOContext *pb = s->pb;
00085     PVAContext *pvactx = s->priv_data;
00086     int syncword, streamid, reserved, flags, length, pts_flag;
00087     int64_t pva_pts = AV_NOPTS_VALUE, startpos;
00088 
00089 recover:
00090     startpos = avio_tell(pb);
00091 
00092     syncword = avio_rb16(pb);
00093     streamid = avio_r8(pb);
00094     avio_r8(pb);               /* counter not used */
00095     reserved = avio_r8(pb);
00096     flags    = avio_r8(pb);
00097     length   = avio_rb16(pb);
00098 
00099     pts_flag = flags & 0x10;
00100 
00101     if (syncword != PVA_MAGIC) {
00102         pva_log(s, AV_LOG_ERROR, "invalid syncword\n");
00103         return AVERROR(EIO);
00104     }
00105     if (streamid != PVA_VIDEO_PAYLOAD && streamid != PVA_AUDIO_PAYLOAD) {
00106         pva_log(s, AV_LOG_ERROR, "invalid streamid\n");
00107         return AVERROR(EIO);
00108     }
00109     if (reserved != 0x55) {
00110         pva_log(s, AV_LOG_WARNING, "expected reserved byte to be 0x55\n");
00111     }
00112     if (length > PVA_MAX_PAYLOAD_LENGTH) {
00113         pva_log(s, AV_LOG_ERROR, "invalid payload length %u\n", length);
00114         return AVERROR(EIO);
00115     }
00116 
00117     if (streamid == PVA_VIDEO_PAYLOAD && pts_flag) {
00118         pva_pts = avio_rb32(pb);
00119         length -= 4;
00120     } else if (streamid == PVA_AUDIO_PAYLOAD) {
00121         /* PVA Audio Packets either start with a signaled PES packet or
00122          * are a continuation of the previous PES packet. New PES packets
00123          * always start at the beginning of a PVA Packet, never somewhere in
00124          * the middle. */
00125         if (!pvactx->continue_pes) {
00126             int pes_signal, pes_header_data_length, pes_packet_length,
00127                 pes_flags;
00128             unsigned char pes_header_data[256];
00129 
00130             pes_signal             = avio_rb24(pb);
00131             avio_r8(pb);
00132             pes_packet_length      = avio_rb16(pb);
00133             pes_flags              = avio_rb16(pb);
00134             pes_header_data_length = avio_r8(pb);
00135 
00136             if (pes_signal != 1) {
00137                 pva_log(s, AV_LOG_WARNING, "expected signaled PES packet, "
00138                                           "trying to recover\n");
00139                 avio_skip(pb, length - 9);
00140                 if (!read_packet)
00141                     return AVERROR(EIO);
00142                 goto recover;
00143             }
00144 
00145             avio_read(pb, pes_header_data, pes_header_data_length);
00146             length -= 9 + pes_header_data_length;
00147 
00148             pes_packet_length -= 3 + pes_header_data_length;
00149 
00150             pvactx->continue_pes = pes_packet_length;
00151 
00152             if (pes_flags & 0x80 && (pes_header_data[0] & 0xf0) == 0x20)
00153                 pva_pts = ff_parse_pes_pts(pes_header_data);
00154         }
00155 
00156         pvactx->continue_pes -= length;
00157 
00158         if (pvactx->continue_pes < 0) {
00159             pva_log(s, AV_LOG_WARNING, "audio data corruption\n");
00160             pvactx->continue_pes = 0;
00161         }
00162     }
00163 
00164     if (pva_pts != AV_NOPTS_VALUE)
00165         av_add_index_entry(s->streams[streamid-1], startpos, pva_pts, 0, 0, AVINDEX_KEYFRAME);
00166 
00167     *pts   = pva_pts;
00168     *len   = length;
00169     *strid = streamid;
00170     return 0;
00171 }
00172 
00173 static int pva_read_packet(AVFormatContext *s, AVPacket *pkt) {
00174     AVIOContext *pb = s->pb;
00175     int64_t pva_pts;
00176     int ret, length, streamid;
00177 
00178     if (read_part_of_packet(s, &pva_pts, &length, &streamid, 1) < 0 ||
00179        (ret = av_get_packet(pb, pkt, length)) <= 0)
00180         return AVERROR(EIO);
00181 
00182     pkt->stream_index = streamid - 1;
00183     pkt->pts = pva_pts;
00184 
00185     return ret;
00186 }
00187 
00188 static int64_t pva_read_timestamp(struct AVFormatContext *s, int stream_index,
00189                                           int64_t *pos, int64_t pos_limit) {
00190     AVIOContext *pb = s->pb;
00191     PVAContext *pvactx = s->priv_data;
00192     int length, streamid;
00193     int64_t res = AV_NOPTS_VALUE;
00194 
00195     pos_limit = FFMIN(*pos+PVA_MAX_PAYLOAD_LENGTH*8, (uint64_t)*pos+pos_limit);
00196 
00197     while (*pos < pos_limit) {
00198         res = AV_NOPTS_VALUE;
00199         avio_seek(pb, *pos, SEEK_SET);
00200 
00201         pvactx->continue_pes = 0;
00202         if (read_part_of_packet(s, &res, &length, &streamid, 0)) {
00203             (*pos)++;
00204             continue;
00205         }
00206         if (streamid - 1 != stream_index || res == AV_NOPTS_VALUE) {
00207             *pos = avio_tell(pb) + length;
00208             continue;
00209         }
00210         break;
00211     }
00212 
00213     pvactx->continue_pes = 0;
00214     return res;
00215 }
00216 
00217 AVInputFormat ff_pva_demuxer = {
00218     .name           = "pva",
00219     .long_name      = NULL_IF_CONFIG_SMALL("TechnoTrend PVA file and stream format"),
00220     .priv_data_size = sizeof(PVAContext),
00221     .read_probe     = pva_probe,
00222     .read_header    = pva_read_header,
00223     .read_packet    = pva_read_packet,
00224     .read_timestamp = pva_read_timestamp
00225 };
Generated on Fri Feb 1 2013 14:34:54 for FFmpeg by doxygen 1.7.1